package com.xxx.service.impl;

import com.xxx.mapper.EmployeeMapper;
import com.xxx.pojo.Employee;
import com.xxx.service.Services;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class EmployeeServiceImpl implements Services<Employee>{


    SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();

    @Override
    public Employee selectByUsername(String username) {
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.selectByUsername(username);
        sqlSession.close();
        return  employee;
    }

    @Override
    public List<Employee> selectAll() {
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employees = mapper.selectAll();
        sqlSession.close();
        return  employees;
    }


    @Override
    public int add(Employee employee) {
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.addEmployee(employee);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int delete(Long id) {
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.delById(id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public Employee selectById(Long id) {
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.selectById(id);
        sqlSession.close();
        return employee;
    }

    @Override
    public int updateById(Employee employee) {
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.updateEmployee(employee);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }


}
